local scheduler = import("framework.scheduler")
local Fish =class("Fish",function () return display.newLayer() end)
function Fish:ctor()
 self.fish = display.newSprite("#xiaochouyu0001.png",0,0);
 self.fishDead =display.newSprite("#xiaochouyu0008.png",0,0);
 local fishFrames =display.newFrames("xiaochouyu00%02d.png",1,7)
 local animation =display.newAnimation(fishFrames, 1.0 / 20) 
 transition.playAnimationForever(self.fish,animation)
 display.setAnimationCache("fishAni",animation)
 self:addChild(self.fish)
 self:addChild(self.fishDead) 
 self.fishDead:setVisible(false)
 self.fishSize =self.fish:getContentSize()
 self.fishX =display.cx -150
 display.align(self.fish,display.CENTER,self.fishX,display.cy);
 display.align(self.fishDead,display.CENTER,self.fishX,display.cy);

end

function Fish:getFishRect()
  local x,y =self.fish:getPosition()
   self.fishSize =self.fish:getContentSize()
  local width =self.fishSize.width-10
  local height =self.fishSize.height-10
   local world_p =self:convertToWorldSpace(CCPoint(x,y)) --local to the world ,when scale it is all right 
   local rect = CCRect(world_p.x-width/2,world_p.y-height/2,width,height)
  return rect
end
function Fish:onTouch()
        
       transition.stopTarget(self.fish)
       local animation =display.getAnimationCache("fishAni")
       print("animation",animation)
       local action =CCAnimate:create(animation)
       self.fish:runAction(CCRepeat:create(action,90000))
       transition.rotateTo(self.fish, {rotate = -30, time = 0})
 end
 

 
function Fish:playShanXian(x,y)
   local shanxian =display.newSprite("#shanxian0001.png",0,0)
   local shanxianframes =display.newFrames("shanxian00%02d.png",1,5)
   local animation =display.newAnimation(shanxianframes, 1.0 / 20)
   self.shanxian =shanxian
   self:addChild(shanxian)
   display.align(shanxian,display.CENTER,x,y)
   transition.playAnimationOnce(shanxian,animation,true)

end 


 
 function Fish:removeEffect()
   transition.stopTarget(self.chongci)
   self:removeChild(self.chongci)
   self:playShanXian(self.fishX,display.cy)
   self.fish:setVisible(true)
   self.fish:setRotation(0)
   display.align(self.fish,display.CENTER,self.fishX,display.cy)
   transition.moveTo(self.fish,{y=100,time=4})
   transition.rotateTo(self.fish,{rotate=80,time=4})
   print("removeEffect Rush")
 end
 


function Fish:collWithDaoJu() --鱼碰到道具
self.fish:setVisible(false)
--   scheduler.performWithDelayGlobal(function ()   self.fish:setVisible(false)  end,0.2)
end

 function Fish:deadEffect() 
   local dead = display.newSprite("#guihun0001.png",0,0);
   local guihunFrames =display.newFrames("guihun00%02d.png",1,14)
   local animation =display.newAnimation(guihunFrames, 1.0 / 14)
   local x,y =self.fish:getPosition()
   dead:setPosition(x,y+150) 
   transition.playAnimationOnce(dead,animation,true,function () self:getParent().gameOverPanel:playAction()  end)
   self:addChild(dead)


end
 function Fish:rotate(t)
   transition.rotateTo(self.fish, {rotate = 80, time = t})
 end
function Fish:size()
   return self.fishSize
end
function Fish:getPosition()
  return self.fish:getPosition()
end

function Fish:setPosition(p)
  self.fish:setPosition(p)
  self.fishDead:setPosition(p)

end

return Fish
